<?php
/**
 * Created by PhpStorm.
 * User: aDaddy
 * Date: 2016/7/25
 * Time: 9:55
 */

namespace Model\SchoolBundle\Model;
use Base\BaseModel;
use Model\SchoolBundle\Setting\SchoolSetting;

class SchoolModel extends BaseModel{

    /*
     * 列表基本设置【通用模块必须开发】
     */
    public function listSettingDefault()
    {
        $settings = new SchoolSetting($this->getDoctrine());
        return $settings->listConfig();
    }

    /*
     * 为后台获取所有文章分类【通用模块必须开发】
     */
    /**
     * @param array $params $param[textKey]=textKey , $param[conditions][table][field][eq|gt|lt...]=value , $param[orderBy][table][field]=DESC|ASC
     * @param int $offset
     * @param int $perPage
     * @return array
     */
    public function getAdminLists($params ,$offset = 0 , $perPage = 10)
    {
        $qb = $this->getQueryFromParam($params);
        $lists = $this->getAllResult($qb,$offset,$perPage);
        $total = $this->countAllResult($qb);

        return array(
            "total" => $total ? $total : 0,
            "data"  => $lists
        );
    }

    public function getList($p = 0 , $perPage = 0)
    {
        $qb = $this->getQueryBuilder();
        $qb->select("*")->from("School");
        $qb->orderBy("id","desc");
        if ($p == 0 && $perPage == 0 ) {//部分页

        } else {//分页获取【暂时不考虑】
            $qb->setFirstResult($p * $perPage);
            $qb->setMaxResults($perPage);
        }
        return $qb->execute()->fetchAll();
    }

    public function addNewApply($params = array())
    {
        $qb = $this->getQueryBuilder();
        //先检查是否有申请记录，有则删除，只保留最新
        $qb->select("*")->from("SchoolApply");
        $qb->where("userId = ?")->setParameter(0,$params["userId"]);
        $num = $qb->execute()->rowCount();
        if ($num) {
            $qb->delete("SchoolApply");
            $qb->execute();
        }
        $defaults = array(
            "type"          => 0,//0=机构，1=个人
            "userId"        => 0,//申请人
            "category"      => 0,//SchoolCategory
            "name"          => "学校名称未设置",
            "logo"          => "/",
            "intro"         => "学校简介未设置",
            "addTime"       => time(),
            "params"        => "[]",//Json 字符串
            "status"        => 0,//0=未审核，1=已审核
        );
        foreach ($defaults as $k => &$v) {
            if (isset($params[$k]) && $params[$k] !== "") {
                $v = $params[$k];
            }
        }
        $qb->insert("SchoolApply");

        $keys = array_keys($defaults);
        $values = array_values($defaults);
        $insertKeys = array();
        foreach ($keys as $k => $val) {
            $insertKeys[$val] = "?";
        }
        $qb->values($insertKeys)->setParameters($values);
        $rst = $qb->execute();
        return $qb->getState();
    }

    public function checkHasSchool($userId)
    {
        $qb = $this->getQueryBuilder();
        $qb->select("*")->from("School");
        $qb->where("userId = ?")->setParameter(0,$userId);
        return $qb->execute()->fetch();
    }

    /**
     * @param $userId
     * @return mixed
     */
    public function getByUserId($userId)
    {
        $qb = $this->getQueryBuilder();
        $qb->select("*")->from("School");
        $qb->where("userId = ?")->setParameter(0,$userId);
        return $qb->execute()->fetch();
    }

    /**
     * @param $id
     * @return mixed
     */
    public function getById($id)
    {
        $qb = $this->getQueryBuilder();
        $qb->select("*")->from("School");
        $qb->where("id = ?")->setParameter(0,$id);
        return $qb->execute()->fetch();
    }

    /**
     * @param int $schoolId
     * @param array $schoolInfo
     */
    public function commUpdate($schoolId , $schoolInfo)
    {
        $qb = $this->getQueryBuilder();
        $qb->update("School");
        foreach ($schoolInfo as $key => $val) {
            $qb->set($key,":$key")->setParameter("$key",$val);
        }
        $qb->where("id = :id")->setParameter("id",$schoolId);
        $qb->execute();
    }

    /**
     * @param $schoolId
     * @param $teacherId
     */
    public function removeTeacher($schoolId,$teacherId)
    {
        $qb = $this->getQueryBuilder();
        $qb->update("Teacher");
        $qb->set("school","?")->setParameter(0,0);
        $qb->where("id = ?")->setParameter(1,$teacherId);
        $qb->andWhere("school = ?")->setParameter(2,$schoolId);
        $qb->execute();
    }
}